package com.webside.front.detail.utils;

public class TokenUtils {
	private static String TAG="TokenUtils";
	public static final String SEED="d$*13?.ghb#!&bk";
	public static final String ENCRY_TYPE_AES="1";
	public static final String ENCRY_TYPE_DES="2";
	
	public static final String SPLIT_CHAR=",";
	
	public static String encodeRequest(String appId, String appKey, String encodeType){
		long requestTimeStamp=System.currentTimeMillis();
		System.out.println(TAG+": encode(appId:"+appId+",appKey:"+appKey+",requestTimeStamp:"+requestTimeStamp+",decodeType:"+encodeType+") start");
		String accessKeyStr=appId+SPLIT_CHAR+appKey+SPLIT_CHAR+requestTimeStamp;
		String accessKey=null;
		if(ENCRY_TYPE_AES.equals(encodeType)){
			System.out.println(TAG+": AES encode start");
			accessKey=AESUtils.encrypt(SEED, accessKeyStr);
			System.out.println(TAG+": AES encode end");
		}else if(ENCRY_TYPE_DES.equals(encodeType)){
			System.out.println(TAG+": DES encode start");
			ThreeDESUtil td = new ThreeDESUtil();
			accessKey=td.getEncString(accessKeyStr, SEED);
			System.out.println(TAG+": DES encode end");
		}
		System.out.println(TAG+": encode() end");
		return accessKey;
	}
	
	/**
	 * @param token (appId,cdnToken,responseTimeStamp)
	 * @param decodeType
	 */
	public static String decodeResponse(String token, String decodeType){
		System.out.println(TAG+": decode(token:"+token+", decodeType:"+decodeType+") start");
		String appInfoStr=null;
		if(ENCRY_TYPE_AES.equals(decodeType)){
			System.out.println(TAG+": AES decode start");
			appInfoStr=AESUtils.decrypt(SEED, token);
			System.out.println(TAG+": AES decode end");
		}else if(ENCRY_TYPE_DES.equals(decodeType)){
			System.out.println(TAG+": DES decode start");
			ThreeDESUtil td = new ThreeDESUtil();
			appInfoStr=td.getDecString(token, SEED);
			System.out.println(TAG+": DES decode end");
		}
		System.out.println(TAG+": decode() end");
		return appInfoStr;
	}
}
